######################################################################
#Replication file for "The Impact of China's AIIB on the World Bank"
#Authors: Jing Qian, James Vreeland, Jianzhi Zhao
#Codes to replicate results in Table C.7
######################################################################
rm(list = ls())

#Other packages used
library(tidyverse)
library(lmtest)
library(sandwich)
library(MASS)
library(stargazer)

#Setwd
setwd("C:/Users/qianj/Dropbox (Princeton)/AIIB_WB_Replication")

#Load custom functions
#Note: Given the number of tests performed in the appendix, 
#several custom functions are created to implement estimation methods in a more efficient way.
#These functions merely create a wrap-up of existing functions, like bpCausal and gsynth,
#in order to avoid typing parameters repeatedly.

source("code/custom_functions.R")

###################################
#Produce Table C.7
###################################
#-----------------------
#(1) Load data
#-----------------------
load("data/data_main.RData")

#-----------------------
#(2) Setup
#-----------------------
#Covariates
covs.all = c("gdppc_log_lag",
             "population_log_lag",
             "election_either_lag",
             "fdi_gdp_lag",
             "debt_gni_lag",
             "oda_gni_lag",
             "polity2_lag",
             "unsc_lag",
             "IdealPointDistance_lag")

#DV
dv.count.list = c("hard_project_count_all")

#FE
fe.list = c("factor(iso2c)",
            "factor(year)")

#Data
df.use = df.main

#Treatment
treat.use = "aiib_founder_2016"

#-----------------------
#(3) Column (1)
#-----------------------
#Setup
covs.use = covs.all

#Equation
equa = make.equation(dv.count.list, treat.use, c(covs.use, fe.list))

#Estimate
fit.tab.c7.1 = lm(equa, df.use)
se.tab.c7.1 = fit.tab.c7.1 %>%
  coeftest(., 
           vcov = vcovCL(., 
                         cluster = df.use$iso2c))

#-----------------------
#(4) Column (2)
#-----------------------
#Setup
covs.use = c(covs.all[1:8],
             "us_friend_lag")
#Equation
equa = make.equation(dv.count.list, treat.use, c(covs.use, fe.list))

#Estimate
fit.tab.c7.2 = lm(equa, df.use)
se.tab.c7.2 = fit.tab.c7.2 %>%
  coeftest(., 
           vcov = vcovCL(., 
                         cluster = df.use$iso2c))

#-----------------------
#(5) Column (3)
#-----------------------
#Setup
covs.use = c("aiib_founder_2016 * IdealPointDistance_lag",
             covs.all)
#Equation
equa = make.equation(dv.count.list, treat.use, c(covs.use, fe.list))

#Estimate
fit.tab.c7.3 = lm(equa, df.use)
se.tab.c7.3 = fit.tab.c7.3 %>%
  coeftest(., 
           vcov = vcovCL(., 
                         cluster = df.use$iso2c))

#-----------------------
#(6) Column (4)
#-----------------------
#Setup
covs.use = c("aiib_founder_2016 * us_friend_lag",
             covs.all[1:8],
             "us_friend_lag")
#Equation
equa = make.equation(dv.count.list, treat.use, c(covs.use, fe.list))

#Estimate
fit.tab.c7.4 = lm(equa, df.use)
se.tab.c7.4 = fit.tab.c7.4 %>%
  coeftest(., 
           vcov = vcovCL(., 
                         cluster = df.use$iso2c))

#-----------------------
#(7) Column (5)
#-----------------------
#Setup
covs.use = c("aiib_founder_2016 * unsc_lag",
             covs.all[1:8],
             "us_friend_lag")

#Equation
equa = make.equation(dv.count.list, treat.use, c(covs.use, fe.list))

#Estimate
fit.tab.c7.5 = lm(equa, df.use)
se.tab.c7.5 = fit.tab.c7.5 %>%
  coeftest(., 
           vcov = vcovCL(., 
                         cluster = df.use$iso2c))

#-----------------------
#(8) Combine
#-----------------------
stargazer(list(fit.tab.c7.1,
               fit.tab.c7.2,
               fit.tab.c7.3,
               fit.tab.c7.4,
               fit.tab.c7.5),
          se = list(se.tab.c7.1[,2],
                    se.tab.c7.2[,2],
                    se.tab.c7.3[,2],
                    se.tab.c7.4[,2],
                    se.tab.c7.5[,2]),
          omit = c("iso2c", "year"),
          type = "text")
